#include <esp_log.h>
#include <esp_err.h>
#include <nvs.h>
#include <nvs_flash.h>
#include <driver/gpio.h>
#include <esp_event.h>

#include "application.h"
#include "system_info.h"

#define TAG "main"

extern "C" void app_main(void)
{
    // 初始化ESP32事件循环(esp_event_loop_create_default)
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    // 初始化NVS闪存(nvs_flash_init)，用于WiFi配置存储
    esp_err_t ret = nvs_flash_init();
    // 处理NVS闪存损坏情况(自动擦除并重新初始化)
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_LOGW(TAG, "Erasing NVS flash to fix corruption");
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    // 启动Application单例(Application::GetInstance().Start())
    Application::GetInstance().Start();
}
